﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FuncChoiceUseCase.cs" company="">
//   
// </copyright>
// <summary>
//   The func choice use case.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.UseCases
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// The func choice use case.
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public class FuncChoiceUseCase<T> : ChoiceUseCase<T>
    {
        #region Fields

        /// <summary>
        ///   The _get func.
        /// </summary>
        private readonly Func<T> _getFunc;

        /// <summary>
        ///   The _get values func.
        /// </summary>
        private readonly Func<IEnumerable<INamedValue<T>>> _getValuesFunc;

        /// <summary>
        ///   The _set action.
        /// </summary>
        private readonly Action<T> _setAction;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="FuncChoiceUseCase{T}"/> class.
        /// </summary>
        /// <param name="parent">
        /// The parent. 
        /// </param>
        /// <param name="service">
        /// The service. 
        /// </param>
        /// <param name="getFunc">
        /// The get func. 
        /// </param>
        /// <param name="setAction">
        /// The set action. 
        /// </param>
        /// <param name="getValuesFunc">
        /// The get values func. 
        /// </param>
        public FuncChoiceUseCase(
            ICompoundUseCase parent, 
            IUseCaseInstanceService service, 
            Func<T> getFunc, 
            Action<T> setAction, 
            Func<IEnumerable<INamedValue<T>>> getValuesFunc)
            : base(parent, service)
        {
            _getFunc = getFunc;
            _setAction = setAction;
            _getValuesFunc = getValuesFunc;
        }

        #endregion

        #region Methods

        /// <summary>
        ///   The get value.
        /// </summary>
        /// <returns> The <see cref="T" /> . </returns>
        protected override T GetValue()
        {
            return _getFunc();
        }

        /// <summary>
        ///   The get values.
        /// </summary>
        /// <returns> The <see cref="IEnumerable" /> . </returns>
        protected override IEnumerable<INamedValue<T>> GetValues()
        {
            return _getValuesFunc();
        }

        /// <summary>
        /// The set value.
        /// </summary>
        /// <param name="value">
        /// The value. 
        /// </param>
        protected override void SetValue(T value)
        {
            _setAction(value);
            Modified = true;
        }

        #endregion
    }
}